#include "MarsPrecompile.h"

#include "MarsPixelShader.h"
#include "MarsRenderer.h"
#include "MarsVRamBuffer.h"

using namespace Graphics;

PixelShader::PixelShader( const char* pcszFileName, const char* pcszEntry, const char* pcszShaderModel )
	: Shader( ShaderType_Pixel, pcszFileName, pcszEntry, pcszShaderModel )
	, m_pPixelShader( NULL )
{
}

PixelShader::~PixelShader()
{
	Release();
}

void PixelShader::Release()
{
	SAFE_RELEASE( m_pPixelShader );

	Shader::Release();
}

bool PixelShader::PostCompile()
{
	HRESULT d3dResult;
	d3dResult = Renderer::Instance().GetDevice()->CreatePixelShader( 
		m_pBuffer->GetBufferPointer(),
		m_pBuffer->GetBufferSize(), 
		0, 
		&m_pPixelShader );

	if ( FAILED( d3dResult ) )
	{
		SAFE_RELEASE( m_pBuffer );
		return false;
	}

	return true;
}

void PixelShader::SetCBufferToContext()
{
	ID3D11DeviceContext* pContext = Renderer::Instance().GetDeviceContext();

	ShaderSlotMapItr it = m_mapShaderSlots.begin();
	ShaderSlotMapItr end = m_mapShaderSlots.end();

	for ( ; it != end; ++it )
	{
		ID3D11Buffer* pBuffer = it->second.VBuffer->GetNativeBuffer();
		pContext->PSSetConstantBuffers( it->second.Index, 1, &pBuffer );
	}
}
